#include <iostream>
#include <string>
#include <unistd.h>
#include <mysql/mysql.h>

const std::string host = "localhost";
const std::string usr = "alpaca";
const std::string passwd = "123456";
const std::string db = "d1";
unsigned int port = 3306;

int main()
{
    // std::cout << mysql_get_client_info() << std::endl;

    MYSQL *my = mysql_init(nullptr);
    if (nullptr == my)
    {
        std::cerr << "init MySQL error" << std::endl;
        return 1;
    }

    if (mysql_real_connect(my, host.c_str(), usr.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        std::cout << "MySQL access error" << std::endl;
        return 2;
    }
    std::cout << "MySQL access success" << std::endl;
    mysql_set_character_set(my, "utf8");

    std::string sql;
    while (true)
    {
        std::cout << "MySQL>>> ";
        if (!std::getline(std::cin, sql) || sql == "quit")
        {
            std::cout << "bye bye" << std::endl;
            break;
        }

        int n = mysql_query(my, sql.c_str());
        if (n == 0)
            std::cout << sql << " success: " << n << std::endl;
        else
            std::cerr << sql << " failed: " << n << std::endl;
    }
    mysql_close(my);
    return 0;
}